#include "includes.h"

#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include "card_data.h"
#include <stdbool.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
// #include <gsl/gsl_rng.h>
// #include <gsl/gsl_randist.h>
#include <string.h>
#include <assert.h>
#include "utilities.h"
// #include <fcntl.h>
#include <unistd.h>




int randomRange(int a, int b, gsl_rng *rng)
{
    // Returns a random integer in the range [a, b]

    if (a < b)
    {
        return gsl_rng_uniform_int(rng, (b - a + 1)) + a;
    }
    else
    {
        return 0;
    }
}


void chooseKingdomCards(enum CARD  *chosenCards, gsl_rng *rng)
{
    // return 10 distinct randomly chosen Kingdom cards
    int i = 0;
    int num_kcards = treasure_map - adventurer + 1;
    enum CARD allKingdomCards[num_kcards];
    for (enum CARD c = adventurer; c <= treasure_map; c++ )
    {
        allKingdomCards[i++] = c;
    }

    gsl_ran_choose(rng, chosenCards, 10, allKingdomCards, num_kcards, sizeof(enum CARD));
}


void chooseCards(unsigned int k, enum CARD *chosenCards, gsl_rng *rng)
{
    // return n cards, not necessarily distinct
    int i = 0;
    int num_cards = treasure_map - curse + 1;
    enum CARD allCards[num_cards];
    for (enum CARD c = curse; c <= treasure_map; c++ )
    {
        allCards[i++] = c;
    }
    gsl_ran_sample(rng, chosenCards, k, allCards, num_cards, sizeof(enum CARD));
}

gsl_rng *rng_setup(unsigned int seed)
{
    // set up random number generator

    const gsl_rng_type *T;
    T = gsl_rng_mt19937;
    gsl_rng *rng = gsl_rng_alloc(T);
    // unsigned int seed = time(0);
    gsl_rng_set(rng, seed);
    return rng;

}

void setChoices(int *choice1, int *choice2, int *choice3, enum CARD card, struct gameState *state, gsl_rng *rng)
// determines valid values for choice1, choice2, and choice3
// TODO -- finish rest of cases
{
    switch (card)
    {
    case feast:
            // choice1 is card to gain from supply, costing up to 5
            for (int target_cost = 5; target_cost >= 0; target_cost--)
            {
                for (enum CARD c = curse; c <= treasure_map; c++)
                {
                    if (cards[c].cost == target_cost && supplyCount(c, state) > 0)
                    {
                        printf("Feast -- gain: %s\n", cards[c].name);
                        *choice1 = c;
                        goto exit1;
                    }
                }
            }

        exit1: break;

    case mine:
        break;

    case remodel:
        break;

    case baron:
        break;

    case minion:
        break;

    case steward:
        break;

    case ambassador:
        break;

    case embargo:
        break;

    case salvager:
        break;

    default:
        break;


    }
}



